﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Windows;
using System.Windows.Media;
using FinPlusCommon;
using log = Logger.Logger;

namespace FinPlusWPFControls
{
    public partial class FinPlusTransform : FinPlusControl
    {
        public override string[] States { get { return new string[] { "RotateAngle", "ScaleValue" }; } }
        public override string[] TestStates { get { return new string[] { "RotateAngle", "ScaleValue" }; } }

        public double ScaleValue { get { return _scaleValue; } set { _scaleValue = value; Load(); } }
        public double RotateAngle { get { return _rotateAngle; } set { _rotateAngle = value; Load(); } }

        private FinPlusWindow _window;
        private FinPlusView _application;
        private bool _hasInitialised;
        private double _rotateAngle, _scaleValue;
        private double _height, _width;

        //construct
        public FinPlusTransform()
        {
            InitializeComponent();
        }
    
        //common control interface 
        public void Initialise()
        {
            try
            {
                if (_hasInitialised) return;

                _window = ControlSearch.FindVisualParent<FinPlusWindow>(this);
                _application = ControlSearch.FindChild<FinPlusView>(_window);

                if (_window == null || _application == null) return;

                _height = _window.Height;
                _width = _window.Width;
                IsValid = _hasInitialised = true;
            }
            catch
            {
                IsValid = false;
            }
        }

        public override void CmdExecute(IFunction command) 
        {
            try
            {
                CmdCapture(command);

                switch ((Cmds)Enum.Parse(typeof(Cmds), command.Name, true))
                {
                    case Cmds.Scale: Scale(); break;
                    case Cmds.Rotate: Rotate(); break;
                    case Cmds.Load: Load(); break;
                    case Cmds.Clear: Clear(); break;
                    default: throw new Exception(string.Format("event not recognised {0}", command.Name));
                }
            }
            catch (CmdExecuteException e)
            {
                log.Log(Logger.Level.Error, Id, Config, e.Message, e);
            }
        }

        //private
        private void Scale()
        {
            _scaleValue = scaleSlider.Value;
            Load();
        }

        private void Rotate()
        {
            var height = _window.Height;
            var width = _window.Width;
            _rotateAngle = _rotateAngle + 90 % 360;
            Load();
            _window.Width = height;
            _window.Height = width;
        }

        private void Load()
        {
            Initialise();

            if (!_hasInitialised) return;

            var group = new TransformGroup();
            group.Children.Add(new RotateTransform(_rotateAngle));
            group.Children.Add(new ScaleTransform(_scaleValue, _scaleValue));
            _application.LayoutTransform = group;
            _application.UpdateLayout();
        }

        private void Clear()
        {
            _scaleValue = 1;
            _rotateAngle = 0;
            _window.Height =  _height;
            _window.Width = _width;
            _application.LayoutTransform = null;
            _application.UpdateLayout();
        }

        //cmds
        private enum Cmds { Scale, Rotate, Load, Clear }

        private void Slider_Changed(object s, RoutedPropertyChangedEventArgs<double> a) { CmdRouter(Cmds.Scale.ToString()); }
        private void Rotate_Click(object s, RoutedEventArgs a) { CmdRouter(Cmds.Rotate.ToString()); }
        private void Control_Loaded(object s, RoutedEventArgs a) { CmdRouter(Cmds.Load.ToString()); }
        private void Clear_Click(object s, RoutedEventArgs a) { CmdRouter(Cmds.Clear.ToString()); }
    }
}














































































































































































//Copyright2013<jnrton>AllRightsReserved 
//Contact j.nrton@finpus